package com.kiln.juxingqs.ui.vm

import android.app.Application
import androidx.lifecycle.MutableLiveData
import client.global.commlib.network.data.JsonResult
import com.blankj.utilcode.util.StringUtils
import com.kiln.juxingqs.api.bean.KDay2
import com.kiln.juxingqs.api.bean.GoodsDetail
import com.kiln.juxingqs.api.bean.KLineHead
import com.kiln.juxingqs.api.bean.KMinute5
import com.kiln.juxingqs.ui.viewmodel.MiddleVm

/**
 * Date: 2023/1/6 12:01
 * Description: k线
 */
class KLineModel(app: Application) : MiddleVm(app) {

    open var begin_time: String = "09:00"
    open var end_time: String = "18:00"

    val isMinuteLoading = MutableLiveData<Boolean>(false)
    val is5dLoading = MutableLiveData<Boolean>(false)

    val isOneLoading = MutableLiveData<Boolean>(false)
    val isTwoLoading = MutableLiveData<Boolean>(false)
    val isThreeLoading = MutableLiveData<Boolean>(false)
    val isFourLoading = MutableLiveData<Boolean>(false)

    val isOpenTime = MutableLiveData<Boolean>(false) //是否是开盘时间

    val goodsDetail = MutableLiveData<JsonResult<GoodsDetail>>()
    val callkLineHead = MutableLiveData<JsonResult<KLineHead>>()

    val callMinute = MutableLiveData<JsonResult<KMinute5>>()
    val call5d = MutableLiveData<JsonResult<KMinute5>>()

    val callkLine = MutableLiveData<JsonResult<KDay2>>()
    val callkLine7 = MutableLiveData<JsonResult<KDay2>>()
    val callkLine30 = MutableLiveData<JsonResult<KDay2>>()
    val callkLine1m = MutableLiveData<JsonResult<KDay2>>()

    val kType = MutableLiveData<Int>(-1)
    val kDate = MutableLiveData<String>("")

    val callOneDayCache = MutableLiveData<String>("") //分时缓存
    val callOneMinuteCache = MutableLiveData<String>("") //1分缓存
//    val callFiveMinuteCache = MutableLiveData<String>("") //5分缓存

    fun holdGoodDetail(id: String) {
        hold({
            server.goods_detail("${id}", "")
        }, {
        }, success = {
            goodsDetail.value = it
        }, complete = {
        })
    }

    fun holeKLine(goods_id: String?, t: Int, date: String?) {
        if (isOneLoading.value!!)
            return
        hold({
            server.goods_k_line_xm(goods_id!!, t, date)
        }, {
            isOneLoading.value = true
        }, success = {

            callkLine.value = it

        }, complete = {
            isOneLoading.value = false
        })
    }


    fun holeKLinemut(goods_id: String?, t: Int, date: String?) {
        if (isFourLoading.value!!)
            return
        hold({
            if (date.isNullOrEmpty()) {
                server.goods_k_line(goods_id!!, t)
            } else {
                server.goods_k_line_xm(goods_id!!, t, date)
            }
        }, {
            isFourLoading.value = true
        }, success = {
            callkLine1m.value = it

        }, complete = {
            isFourLoading.value = false
        })
    }


    fun holeKLine7(goods_id: String?, t: Int, date: String?) {
        if (isTwoLoading.value!!)
            return
        hold({
            server.goods_k_line_xm(goods_id!!, t, date)
        }, {
            isTwoLoading.value = true
        }, success = {
            callkLine7.value = it

        }, complete = {
            isTwoLoading.value = false
        })
    }


    fun holeKLine30(goods_id: String?, t: Int, date: String?) {
        if (isThreeLoading.value!!)
            return
        hold({
            server.goods_k_line_xm(goods_id!!, t, date)
        }, {
            isThreeLoading.value = true
        }, success = {
            callkLine30.value = it
        }, complete = {
            isThreeLoading.value = false
        })
    }

    fun holeOneDay(goods_id: String?, t: Int) {
        if (isMinuteLoading.value!!)
            return
        hold({
            server.goods_kline_oneday_5day(goods_id!!, t)
        }, {
            isMinuteLoading.value = true
        }, success = {
            callMinute.value = it
        }, complete = {
            isMinuteLoading.value = false
        })
    }

    fun hole5Day(goods_id: String?, t: Int) {
        if (is5dLoading.value!!)
            return
        hold({
            server.goods_kline_oneday_5day(goods_id!!, t)
        }, {
            is5dLoading.value = true
        }, success = {
            call5d.value = it
        }, complete = {
            is5dLoading.value = false
        })
    }

//    fun holeKLine5d(goods_id: String?) {
//        if (isK5dLoading.value!!)
//            return
//        hold({
//            server.goods_k_line_minute5(goods_id!!, kType.value!!)
//        }, {
//            isK5dLoading.value = true
//        }, success = {
//            callkLine5d.value = it
//        }, complete = {
//            isK5dLoading.value = false
//        })
//    }

    fun holeKLineHead(goods_id: String?) {
        hold({
            server.goods_k_line_head(goods_id!!)
        }, success = {
            callkLineHead.value = it
        })

/*//        if (isOneLoading.value!!)
//            return
        hold({
            server.goods_k_line_head(goods_id!!)
        }, {
//            isOneLoading.value = true
        }, success = {
            callkLineHead.value = it
        }, complete = {
//            isOneLoading.value = false
        })*/
    }

}